package com.quan.system.web.controller;

import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.quan.common.base.bean.ResultBean;
import com.quan.elasticjob.handler.ElasticJobHandler;
import com.quan.elasticjob.job.SimpleElasticJob;
import com.quan.system.entity.JobTask;
import com.quan.system.service.JobTaskService;

@RestController
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private RabbitTemplate rabbitTemplate ;
	
	@Value("${server.port}")
	private String port ;
	
	@Autowired
	private CoordinatorRegistryCenter registryCenter ;
	
	@Autowired
	private ElasticJobHandler elasticJobHandler ;
	
	@Autowired
	private JobTaskService taskService ;
	
	@GetMapping(value = "/scheduler01")
	public Object scheduler01() {
		System.out.println(SimpleElasticJob.class.getCanonicalName());
		// 定义作业核心配置
		JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("simpleJob-01", "0/5 * * * * ?", 1).jobParameter("20180001").build();
		// 定义SIMPLE类型配置
		SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, SimpleElasticJob.class.getCanonicalName());
		// 定义Lite作业根配置
		LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
		//启动作业
		new JobScheduler(registryCenter, liteJobConfiguration).init();	
		
		return ResultBean.ok() ;
	}
	
	@GetMapping(value = "/scheduler02")
	public Object scheduler02() {
		// 定义作业核心配置
		JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("simpleJob-02", "0/5 * * * * ?", 1).jobParameter("20180002").build();
		// 定义SIMPLE类型配置
		SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, SimpleElasticJob.class.getCanonicalName());
		// 定义Lite作业根配置
		LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
		//启动作业
		new JobScheduler(registryCenter, liteJobConfiguration).init();	
		
		return ResultBean.ok() ;
	}
	
	@GetMapping(value = "/scheduler03")
	public Object scheduler03() {
		this.elasticJobHandler.addJob("SendMialJob-01", "0/5 * * * * ?", 1, SimpleElasticJob.class, "定时发送邮件--01");
		return ResultBean.ok() ;
	}
	
	@GetMapping(value = "/scheduler04")
	public Object scheduler04() {
		this.elasticJobHandler.addJob("SendMialJob-02", "0/5 * * * * ?", 1, SimpleElasticJob.class.getCanonicalName(), "定时发送邮件--02");
		return ResultBean.ok() ;
	}
	
	@GetMapping(value = "/scheduler05")
	public Object scheduler05() {
		JobTask entity = new JobTask() ;
		entity.setJobName("发送邮件");
		entity.setJobClass(SimpleElasticJob.class.getCanonicalName());
		entity.setCron("0/10 * * * * ?");
		this.taskService.insertSelective(entity) ;
		return ResultBean.ok() ;
	}

	@GetMapping(value = "/config")
	public Object realConfig() {
		System.out.println("读取仓库中的配置文件>>>>>["+port+"]");
		System.out.println(rabbitTemplate);
		
		return null ;
	}
	
	@PostMapping(value = "/upload")
	public Object uploadAvatar(HttpServletRequest request, String ossName, String bucketName, String prefixName) {
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

			// 取得MultiValueMap集合
			MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
			Set<Entry<String, List<MultipartFile>>> entrySet = multiFileMap.entrySet();
			for (Entry<String, List<MultipartFile>> entry : entrySet) {

				// 获取MultipartFile集合
				List<MultipartFile> multipartFiles = entry.getValue();
				for (MultipartFile multipartFile : multipartFiles) {
					System.out.println(multipartFile.getOriginalFilename());
					return ResultBean.ok();
				}
			}
		} catch (Exception e) {
			return ResultBean.errorMsg("文件上传失败") ;
		}
		return ResultBean.ok();
	}
	
}
